package com.example.admin.goodnews.fragment;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.adapter.MoreChannelQuanBuAdapter;
import com.example.admin.goodnews.adapter.MoreChannelYiXuanAdapter;
import com.example.admin.goodnews.choosecity.MyGridView;
import com.example.admin.goodnews.database.MyDBHelper;
import com.example.admin.goodnews.database.MyDBManager;
import com.example.admin.goodnews.entity.NewsClassEntity;
import com.example.admin.goodnews.fragment.XinWenFenLei.CBAZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.CaiJingJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.CaiJingZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.DianNaoZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.DianShiZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.DianYingZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.FangChanJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.FangChanZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.GangAoZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.GuoJiJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.GuoJiZuQiuZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.GuoJiZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.GuoNeiJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.GuoNeiZuQiuZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.GuoNeiZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.HongGuanJingJiZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.HuLianWangJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.HuLianWangZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.JianKangYangShengZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.JiaoYuJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.JiaoYuZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.JunShiJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.JunShiZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.KeJiJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.KeJiZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.KePuZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.LiCaiZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.MeiRongHuFuZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.NvRenJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.NvRenZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.QiCheJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.QiCheZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.QingGangLiangXingZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.SheHuiJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.SheHuiZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.ShuMaZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.TaiWanZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.TiYuJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.TiYuZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.YouXiJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.YouXiZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.YuLeJiaoDianFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.YuLeZuiXinFragment;
import com.example.admin.goodnews.fragment.XinWenFenLei.ZongHeTiYuZuiXinFragment;
import com.example.admin.goodnews.utils.HttpUtils;
import com.example.admin.goodnews.utils.NetWorkDataUrl;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * 更多频道 Fragment
 * Created by admin on 2016/9/30.
 */

public class MoreChannelFragment extends Fragment {
    private MyGridView mygridview_yixuan;// 已选频道
    private MyGridView mygridview_quanbu;// 全部频道
    private NewsClassEntity newsClassEntity;
    private List<String> defaultlists = new ArrayList<>();// 默认已选频道分类
    private List<Fragment> defaultfragmentlists = new ArrayList<>();// 默认已选频道页卡
    private List<String> lists = new ArrayList<>();// 显示已选频道用的列表
    private List<String> alllists = new ArrayList<>();// 全部新闻频道分类
    private List<Fragment> allfragmentlists = new ArrayList<>();// 全部新闻频道页卡
    private MoreChannelYiXuanAdapter moreChannelYiXuanAdapter;// 已选频道数据适配器
    private MoreChannelQuanBuAdapter moreChannelQuanBuAdapter;// 全部频道数据适配器

    private MyDBHelper dbhelper;
    private SQLiteDatabase db;
    private MyDBManager dbmanager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.activity_channelmanage, null);

        mygridview_yixuan = (MyGridView) view.findViewById(R.id.mygridview_yixuan);
        mygridview_quanbu = (MyGridView) view.findViewById(R.id.mygridview_quanbu);

        setdefault();// 设置默认新闻分类
        bindData();
        return view;
    }

    // 设置默认新闻分类
    private void setdefault() {
        dbhelper = new MyDBHelper(getActivity());
        db = dbhelper.getWritableDatabase();
        dbmanager = new MyDBManager(dbhelper, db);

        defaultlists.add("国内焦点");
        defaultlists.add("国际焦点");
        defaultlists.add("军事焦点");
        defaultlists.add("财经焦点");
        defaultlists.add("体育焦点");
        defaultlists.add("娱乐焦点");
        defaultlists.add("教育焦点");
        defaultlists.add("科技焦点");

        defaultfragmentlists.add(new GuoNeiJiaoDianFragment());// 国内焦点
        defaultfragmentlists.add(new GuoJiJiaoDianFragment());// 国际焦点
        defaultfragmentlists.add(new JunShiJiaoDianFragment());// 军事焦点
        defaultfragmentlists.add(new CaiJingJiaoDianFragment());// 财经焦点
        defaultfragmentlists.add(new TiYuJiaoDianFragment());// 体育焦点
        defaultfragmentlists.add(new YuLeJiaoDianFragment());// 娱乐焦点
        defaultfragmentlists.add(new JiaoYuJiaoDianFragment());// 教育焦点
        defaultfragmentlists.add(new KeJiJiaoDianFragment());// 科技焦点

        for (int i = 0; i < defaultlists.size(); i++) {
            dbmanager.insertdata(defaultlists.get(i));
        }
        lists = dbmanager.querydata();
        moreChannelYiXuanAdapter = new MoreChannelYiXuanAdapter(getActivity(), lists);
        mygridview_yixuan.setAdapter(moreChannelYiXuanAdapter);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 已选频道
        mygridview_yixuan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 点击已选频道下的任一一个分类可将其从已选频道分类下删除
                dbmanager.deletedata(moreChannelYiXuanAdapter.getItem(position).toString());
                lists = dbmanager.querydata();
                moreChannelYiXuanAdapter = new MoreChannelYiXuanAdapter(getActivity(), lists);
                mygridview_yixuan.setAdapter(moreChannelYiXuanAdapter);
                moreChannelYiXuanAdapter.notifyDataSetChanged();
            }
        });
        // 全部频道
        mygridview_quanbu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 点击全部频道下的任一一个分类可添加至已选频道分类下
                dbmanager.insertdata(moreChannelQuanBuAdapter.getItem(position).toString());
                lists = dbmanager.querydata();
                moreChannelYiXuanAdapter = new MoreChannelYiXuanAdapter(getActivity(), lists);
                mygridview_yixuan.setAdapter(moreChannelYiXuanAdapter);
                moreChannelYiXuanAdapter.notifyDataSetChanged();
            }
        });
    }

    // 全部新闻分类
    private void bindData() {
        try {
            HttpUtils.doPostAsyn(NetWorkDataUrl.ALLWEBURL_url, NetWorkDataUrl.ALLWEBURL_PARAM, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    newsClassEntity = gson.fromJson(result, NewsClassEntity.class);
                    if (newsClassEntity.getShowapi_res_body().getChannelList().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    //Snackbar.make(relativelayout_quanbuxinwen, "服务器君离家出走了~", Snackbar.LENGTH_SHORT).show();
                    break;
                case 1:
                    allfragmentlists.add(new GuoNeiJiaoDianFragment());// 国内焦点
                    allfragmentlists.add(new GuoJiJiaoDianFragment());// 国际焦点
                    allfragmentlists.add(new JunShiJiaoDianFragment());// 军事焦点
                    allfragmentlists.add(new CaiJingJiaoDianFragment());// 财经焦点
                    allfragmentlists.add(new HuLianWangJiaoDianFragment());// 互联网焦点
                    allfragmentlists.add(new FangChanJiaoDianFragment());// 房产焦点
                    allfragmentlists.add(new QiCheJiaoDianFragment());// 汽车焦点
                    allfragmentlists.add(new TiYuJiaoDianFragment());// 体育焦点
                    allfragmentlists.add(new YuLeJiaoDianFragment());// 娱乐焦点
                    allfragmentlists.add(new YouXiJiaoDianFragment());// 游戏焦点
                    allfragmentlists.add(new JiaoYuJiaoDianFragment());// 教育焦点
                    allfragmentlists.add(new NvRenJiaoDianFragment());// 女人焦点
                    allfragmentlists.add(new KeJiJiaoDianFragment());// 科技焦点
                    allfragmentlists.add(new SheHuiJiaoDianFragment());// 社会焦点
                    allfragmentlists.add(new GuoNeiZuiXinFragment());// 国内最新
                    allfragmentlists.add(new TaiWanZuiXinFragment());// 台湾最新
                    allfragmentlists.add(new GangAoZuiXinFragment());// 港澳最新
                    allfragmentlists.add(new GuoJiZuiXinFragment());// 国际最新
                    allfragmentlists.add(new JunShiZuiXinFragment());// 军事最新
                    allfragmentlists.add(new CaiJingZuiXinFragment());// 财经最新
                    allfragmentlists.add(new LiCaiZuiXinFragment());// 理财最新
                    allfragmentlists.add(new HongGuanJingJiZuiXinFragment());// 宏观经济最新
                    allfragmentlists.add(new HuLianWangZuiXinFragment());// 互联网最新
                    allfragmentlists.add(new FangChanZuiXinFragment());// 房产最新
                    allfragmentlists.add(new QiCheZuiXinFragment());// 汽车最新
                    allfragmentlists.add(new TiYuZuiXinFragment());// 体育最新
                    allfragmentlists.add(new GuoJiZuQiuZuiXinFragment());// 国际足球最新
                    allfragmentlists.add(new GuoNeiZuQiuZuiXinFragment());// 国内足球最新
                    allfragmentlists.add(new CBAZuiXinFragment());// CBA最新
                    allfragmentlists.add(new ZongHeTiYuZuiXinFragment());// 综合体育最新
                    allfragmentlists.add(new YuLeZuiXinFragment());// 娱乐最新
                    allfragmentlists.add(new DianYingZuiXinFragment());// 电影最新
                    allfragmentlists.add(new DianShiZuiXinFragment());// 电视最新
                    allfragmentlists.add(new YouXiZuiXinFragment());// 游戏最新
                    allfragmentlists.add(new JiaoYuZuiXinFragment());// 教育最新
                    allfragmentlists.add(new NvRenZuiXinFragment());// 女人最新
                    allfragmentlists.add(new MeiRongHuFuZuiXinFragment());// 美容护肤最新
                    allfragmentlists.add(new QingGangLiangXingZuiXinFragment());// 情感两性最新
                    allfragmentlists.add(new JianKangYangShengZuiXinFragment());// 健康养生最新
                    allfragmentlists.add(new KeJiZuiXinFragment());// 科技最新
                    allfragmentlists.add(new ShuMaZuiXinFragment());// 数码最新
                    allfragmentlists.add(new DianNaoZuiXinFragment());// 电脑最新
                    allfragmentlists.add(new KePuZuiXinFragment());// 科普最新
                    allfragmentlists.add(new SheHuiZuiXinFragment());// 社会最新

                    for (int i = 0; i < newsClassEntity.getShowapi_res_body().getChannelList().size(); i++) {
                        alllists.add(newsClassEntity.getShowapi_res_body().getChannelList().get(i).getName());
                    }
                    moreChannelQuanBuAdapter = new MoreChannelQuanBuAdapter(getActivity(), alllists);
                    mygridview_quanbu.setAdapter(moreChannelQuanBuAdapter);
                    break;
                case 2:

                    break;
            }
        }
    };

}
